﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Criterion;
namespace ServiceGerenciador.Model
{
    [Serializable]
    [ActiveRecord("Usuarios")]
    public class User : ActiveRecordBase<User>
    {
        public User()
        {

        }        

        [PrimaryKey]
        public int Id { get; set; }
        [BelongsTo("IdPessoa")]
        public Person Pessoa { get; set; }
        [Property]
        public string Usuario { get; set; }
        [Property]
        public string Senha { get; set; }
        [Property]
        public DateTime? UltimoLogin { get; set; }

        public string Nome
        {
            get
            {
                if (this.Pessoa != null)
                    return this.Pessoa.Nome;
                else
                    return string.Empty;
            }
        }

        [Property("Admin")]
        public bool EhAdmin { get; set; }

        public static User DoLogin(string usernome, string pass)
        {
            var user = FindAllByProperty("Usuario", usernome).FirstOrDefault();

            if (user != null)
            {
                if (user.Senha.Equals(pass))
                    return user;
            }

            return null;
        }

        public static User[] Pesquisa(String termo)
        {
            var users = FindAll();
            return users.Where((u) => (u.Usuario.ToUpper().Contains(termo.ToUpper()))).ToArray();                                           
        }      
    }
}
